package org.dromara.dp.config;

import jakarta.annotation.PostConstruct;
import org.dromara.dp.vo.NodeMapVo;
import org.dromara.dp.vo.NodeRelationVo;
import org.dromara.dp.vo.NodeVo;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class NodeMapConfig {

    private static final Map<Long, NodeMapVo> NODE_MAP_CACHE = new HashMap<>();

    @PostConstruct
    public void init() {
        // 甲醇缓冲罐
        NODE_MAP_CACHE.put(1629400123456789034L, createDevice0());
        // 氢气分解变换反应器
        NODE_MAP_CACHE.put(1629400123456789035L, createDevice2());
        // 反应器
        NODE_MAP_CACHE.put(1629400123456789036L, createDevice3());
        // 吸附塔
        NODE_MAP_CACHE.put(1629400123456789037L, createDevice4());
        // 吸附塔T-9732
        NODE_MAP_CACHE.put(1629400123456789038L, createDevice5());
        // V-9733
        NODE_MAP_CACHE.put(1629400123456789039L, createDevice6());
        // V-9734
        NODE_MAP_CACHE.put(1629400123456789040L, createDevice7());
        // V-9735
        NODE_MAP_CACHE.put(1629400123456789041L, createDevice8());
        // 导热油炉炉管
        NODE_MAP_CACHE.put(1629400123456789042L, createDevice9());
        // 燃料气
        NODE_MAP_CACHE.put(1629400123456789043L, createDevice10());
        // 二氧化碳
        NODE_MAP_CACHE.put(1629400123456789044L, createDevice11());
    }

    private NodeMapVo createDevice0() {
        NodeVo n0 = new NodeVo(8L, "调节阀全开", 2, "LV970101", "进料管线", "流量过大");
        NodeVo n1 = new NodeVo(9L, "泵状态异常", 2, "P9701", "甲醇输送泵", "流量过大");
        NodeVo n2 = new NodeVo(10L, "阀门开度异常", 2, "PV970101A", "PV970101A", "开度过大");

        NodeVo n3 = new NodeVo(11L, "阀门开度异常", 2, "PV970101B", "PV970101B", "开度过小");
        NodeVo n4 = new NodeVo(12L, "液位过高", 2, "LT970101", "甲醇缓冲罐", "液位过高");
        NodeVo n5 = new NodeVo(13L, "压力过高", 2, "PT970101", "甲醇缓冲罐", "压力过高");
        n0.setBaseValues(List.of("20%", "35%", "40%", "55%"));
        n1.setBaseValues(List.of("0.2m³/s", "0.5m³/s", "0.6m³/s", "0.9m³/s"));
        n2.setBaseValues(List.of("25%", "55%", "65%", "85"));
        n3.setBaseValues(List.of("15%", "10%", "7%", "5%"));
        n4.setBaseValues(List.of("2500mm", "3000mm", "3200mm", "3800mm"));
        n5.setBaseValues(List.of("0.5MP", "1.0MP", "1.1MP", "1.3MP"));

        List<NodeVo> nodes = List.of(
            new NodeVo(1L, "进料调节阀全开", 1, "", "", "人员操作"),
            new NodeVo(2L, "进料调节阀故障", 1, "", "LV970101", "外部错误"),
            new NodeVo(3L, "进料调节控制器故障", 1, "", "LICA970101", "机械故障"),
            new NodeVo(4L, "甲醇输送泵异常", 1, "", "p-9701", "机械故障"),
            new NodeVo(5L, "调节阀意外关阀", 1, "", "FV-970102", "内部错误"),
            new NodeVo(6L, "流量控制器故障", 1, "", "FIQ970104", "机械故障"),
            new NodeVo(7L, "压力调节阀故障", 1, "", "PICA970101", "机械故障"),
            n0, n1, n2, n3, n4, n5,
            new NodeVo(14L, "V-9701甲醇缓冲罐泄露", 4),
            new NodeVo(15L, "甲醇缓冲罐泄露发生火灾", 5, "", "", "", "5D", 1, "5F", 2, null),
            new NodeVo(16L, "甲醇缓冲罐泄露发生人员中毒", 5, "", "", "", "5D", 1, "5F", 2, null)
        );

        List<NodeRelationVo> relations = List.of(
            new NodeRelationVo(1L, 8L), new NodeRelationVo(2L, 8L), new NodeRelationVo(3L, 8L),
            new NodeRelationVo(4L, 9L), new NodeRelationVo(5L, 9L), new NodeRelationVo(6L, 9L),
            new NodeRelationVo(7L, 10L), new NodeRelationVo(8L, 12L), new NodeRelationVo(9L, 12L),
            new NodeRelationVo(10L, 11L), new NodeRelationVo(11L, 13L),

            new NodeRelationVo(12L, 14L),
            new NodeRelationVo(13L, 14L),
            new NodeRelationVo(14L, 15L), new NodeRelationVo(14L, 16L)
        );

        return createNodeMap(nodes, relations);
    }

    private NodeMapVo createDevice1() {
        List<NodeVo> nodes = List.of(
            new NodeVo(1L, "人员误操作", null),
            new NodeVo(2L, "LV970101故障", null),
            new NodeVo(3L, "LICA970101故障", null),
            new NodeVo(4L, "P-970101故障", null),
            new NodeVo(5L, "FV-970102阀关", null),
            new NodeVo(6L, "FIQ970104故障", null),
            new NodeVo(7L, "LV-970101全开", null),
            new NodeVo(8L, "P-9701泵故障", null),
            new NodeVo(9L, "PICA-970101致PV970101A全开，PV970101B关闭", null),
            new NodeVo(10L, "人员误操作", null),
            new NodeVo(11L, "液位上升", null),
            new NodeVo(12L, "氮气压力高", null),
            new NodeVo(13L, "V-9701泄漏", null),
            new NodeVo(14L, "1.界区内1~2人轻伤，因事故伤害,需要住院，出现损工或限工，但是没有严重后果2.界区内3人以上轻伤，需要去医院处置，医疗事件，不需住院，无损工或限工;3.界区外1~2人以上出现需要到专业医院，进行医疗处置，无住院，无损工或限工:4.界区外3人以上出现First Aid或医护事件:", 5),
            new NodeVo(15L, "中等影响的健康事故:1.部分性的受损或伤残，例如:视力/听力减退;2.非永久性、一定时期内的负面健康影响3.暴露于一个有较高职业病危害因素的场所;", 5)
        );

        List<NodeRelationVo> relations = List.of(
            new NodeRelationVo(1L, 7L), new NodeRelationVo(2L, 7L), new NodeRelationVo(3L, 7L),
            new NodeRelationVo(4L, 8L), new NodeRelationVo(5L, 8L), new NodeRelationVo(6L, 8L),
            new NodeRelationVo(7L, 11L), new NodeRelationVo(8L, 11L), new NodeRelationVo(9L, 12L),
            new NodeRelationVo(10L, 12L), new NodeRelationVo(11L, 13L), new NodeRelationVo(12L, 13L),
            new NodeRelationVo(13L, 14L), new NodeRelationVo(13L, 15L)
        );

        return createNodeMap(nodes, relations);
    }

    private NodeMapVo createDevice2() {
        List<NodeVo> nodes = List.of(
            new NodeVo(16L, "人员误操作", null),
            new NodeVo(17L, "PV970304A故障", null),
            new NodeVo(18L, "PIC970304故障", null),
            new NodeVo(19L, "FIQ970304故障", null),
            new NodeVo(20L, "PIC970304B故障", null),
            new NodeVo(21L, "PV970304A关闭", null),
            new NodeVo(22L, "下游PSA装置故障，装置进气阀关闭", null),
            new NodeVo(23L, "对应的上游系统内压力升高", null),
            new NodeVo(24L, "氢气分解变换反应器R9711/9721；换热器R9711-4/9721-4等及此部分涉氢管线等的泄漏", null),
            new NodeVo(25L, "超压泄漏", null)
        );

        List<NodeRelationVo> relations = List.of(
            new NodeRelationVo(16L, 21L), new NodeRelationVo(17L, 21L), new NodeRelationVo(18L, 21L),
            new NodeRelationVo(19L, 21L), new NodeRelationVo(20L, 21L), new NodeRelationVo(21L, 23L),
            new NodeRelationVo(22L, 23L), new NodeRelationVo(23L, 24L), new NodeRelationVo(24L, 25L)
        );

        return createNodeMap(nodes, relations);
    }

    private NodeMapVo createDevice3() {
        List<NodeVo> nodes = List.of(
            new NodeVo(26L, "人员误操作", null),
            new NodeVo(27L, "PV970304A故障", null),
            new NodeVo(28L, "PIC970304故障", null),
            new NodeVo(29L, "FIQ970304故障", null),
            new NodeVo(30L, "PIC970304B故障", null),
            new NodeVo(31L, "PV970304A关闭", null),
            new NodeVo(32L, "下游PSA装置故障，装置进气阀关闭", null),
            new NodeVo(33L, "PV970304A故障关", null),
            new NodeVo(34L, "SV9702进口阀关闭", null),
            new NodeVo(35L, "V-9704分液罐泄漏", null),
            new NodeVo(36L, "超压泄漏", null)
        );

        List<NodeRelationVo> relations = List.of(
            new NodeRelationVo(26L, 31L), new NodeRelationVo(27L, 31L), new NodeRelationVo(28L, 31L),
            new NodeRelationVo(29L, 31L), new NodeRelationVo(30L, 31L), new NodeRelationVo(31L, 35L),
            new NodeRelationVo(32L, 35L), new NodeRelationVo(33L, 35L), new NodeRelationVo(34L, 35L),
            new NodeRelationVo(35L, 36L)
        );

        return createNodeMap(nodes, relations);
    }

    private NodeMapVo createDevice4() {
        List<NodeVo> nodes = List.of(
            new NodeVo(37L, "人员误操作", null),
            new NodeVo(38L, "PICA970402故障", null),
            new NodeVo(39L, "PV970402关", null),
            new NodeVo(40L, "吸附塔出口KV970402故障关", null),
            new NodeVo(41L, "PV970402故障关", null),
            new NodeVo(42L, "压力升高", null),
            new NodeVo(43L, "吸附塔泄漏", null),
            new NodeVo(44L, "超压泄漏", null)
        );

        List<NodeRelationVo> relations = List.of(
            new NodeRelationVo(37L, 39L), new NodeRelationVo(38L, 39L), new NodeRelationVo(39L, 42L),
            new NodeRelationVo(40L, 42L), new NodeRelationVo(41L, 42L), new NodeRelationVo(42L, 43L),
            new NodeRelationVo(43L, 44L)
        );

        return createNodeMap(nodes, relations);
    }

    private NodeMapVo createDevice5() {
        List<NodeVo> nodes = List.of(
            new NodeVo(45L, "吸附塔出口程控阀门KV-970502故障关", null),
            new NodeVo(46L, "阀位故障状态下的程序保护失效", null),
            new NodeVo(47L, "安全阀SV-9732失效", null),
            new NodeVo(48L, "压力升高", null),
            new NodeVo(49L, "吸附塔泄漏", null),
            new NodeVo(50L, "超压泄漏", null)
        );

        List<NodeRelationVo> relations = List.of(
            new NodeRelationVo(45L, 48L), new NodeRelationVo(46L, 48L), new NodeRelationVo(47L, 48L),
            new NodeRelationVo(48L, 49L), new NodeRelationVo(49L, 50L)
        );

        return createNodeMap(nodes, relations);
    }

    private NodeMapVo createDevice6() {
        List<NodeVo> nodes = List.of(
            new NodeVo(51L, "人员误操作", null),
            new NodeVo(52L, "PV970502故障", null),
            new NodeVo(53L, "PIC-970502故障", null),
            new NodeVo(54L, "吸附过程中KV-970509故障开", null),
            new NodeVo(55L, "阀位故障状态下的程序保护失效", null),
            new NodeVo(56L, "安全阀SV-9734失效", null),
            new NodeVo(57L, "PV970502放空阀关闭", null),
            new NodeVo(58L, "压力升高", null),
            new NodeVo(59L, "吸附塔泄漏", null),
            new NodeVo(60L, "超压泄漏", null)
        );

        List<NodeRelationVo> relations = List.of(
            new NodeRelationVo(51L, 57L), new NodeRelationVo(52L, 57L), new NodeRelationVo(53L, 57L),
            new NodeRelationVo(54L, 58L), new NodeRelationVo(55L, 58L), new NodeRelationVo(56L, 58L),
            new NodeRelationVo(57L, 58L), new NodeRelationVo(58L, 59L), new NodeRelationVo(59L, 60L)
        );

        return createNodeMap(nodes, relations);
    }

    private NodeMapVo createDevice7() {
        List<NodeVo> nodes = List.of(
            new NodeVo(61L, "人员误操作", null),
            new NodeVo(62L, "PV970503故障", null),
            new NodeVo(63L, "PIC-970503故障", null),
            new NodeVo(64L, "吸附塔入口程控阀门KV-970507故障关", null),
            new NodeVo(65L, "阀位故障状态下的程序保护失效", null),
            new NodeVo(66L, "安全阀SV-9735失效", null),
            new NodeVo(67L, "PV－970503放空阀关闭", null),
            new NodeVo(68L, "压力升高", null),
            new NodeVo(69L, "吸附塔泄漏", null),
            new NodeVo(70L, "超压泄漏", null)
        );

        List<NodeRelationVo> relations = List.of(
            new NodeRelationVo(61L, 67L), new NodeRelationVo(62L, 67L), new NodeRelationVo(63L, 67L),
            new NodeRelationVo(64L, 68L), new NodeRelationVo(65L, 68L), new NodeRelationVo(66L, 68L),
            new NodeRelationVo(67L, 68L), new NodeRelationVo(68L, 69L), new NodeRelationVo(69L, 70L)
        );

        return createNodeMap(nodes, relations);
    }

    private NodeMapVo createDevice8() {
        List<NodeVo> nodes = List.of(
            new NodeVo(71L, "人员误操作", null),
            new NodeVo(72L, "PV970504故障", null),
            new NodeVo(73L, "PIC-970504故障", null),
            new NodeVo(74L, "吸附塔入口程控阀门KV-970508故障关", null),
            new NodeVo(75L, "阀位故障状态下的程序保护失效", null),
            new NodeVo(76L, "安全阀SV-9736失效", null),
            new NodeVo(77L, "PV－970504放空阀关闭", null),
            new NodeVo(78L, "压力升高", null),
            new NodeVo(79L, "吸附塔泄漏", null),
            new NodeVo(80L, "超压泄漏", null)
        );

        List<NodeRelationVo> relations = List.of(
            new NodeRelationVo(71L, 77L), new NodeRelationVo(72L, 77L), new NodeRelationVo(73L, 77L),
            new NodeRelationVo(74L, 78L), new NodeRelationVo(75L, 78L), new NodeRelationVo(76L, 78L),
            new NodeRelationVo(77L, 78L), new NodeRelationVo(78L, 79L), new NodeRelationVo(79L, 80L)
        );

        return createNodeMap(nodes, relations);
    }

    private NodeMapVo createDevice9() {
        List<NodeVo> nodes = List.of(
            new NodeVo(81L, "人员误操作", null),
            new NodeVo(82L, "P-9712(9722)A-C故障", null),
            new NodeVo(83L, "局部电源故障", null),
            new NodeVo(84L, "人员误操作", null),
            new NodeVo(85L, "运行温度过高", null),
            new NodeVo(86L, "运行周期过长", null),
            new NodeVo(87L, "下游用户突然降低使用负荷", null),
            new NodeVo(88L, "导热油循环泵P-9712(9722)A-C故障停", null),
            new NodeVo(89L, "炉出口手阀关闭", null),
            new NodeVo(90L, "炉管结焦", null),
            new NodeVo(91L, "导热油输出量减少", null),
            new NodeVo(92L, "导热油进料中断", null),
            new NodeVo(93L, "导热油出料中断，炉管憋压", null),
            new NodeVo(94L, "炉管堵塞", null),
            new NodeVo(95L, "炉管憋压，压力升高", null),
            new NodeVo(96L, "导热油炉炉管损坏", null),
            new NodeVo(97L, "泄漏火灾", null)
        );

        List<NodeRelationVo> relations = List.of(
            new NodeRelationVo(81L, 88L), new NodeRelationVo(82L, 88L), new NodeRelationVo(83L, 88L),
            new NodeRelationVo(84L, 89L), new NodeRelationVo(85L, 90L), new NodeRelationVo(86L, 90L),
            new NodeRelationVo(87L, 91L), new NodeRelationVo(88L, 92L), new NodeRelationVo(89L, 93L),
            new NodeRelationVo(90L, 94L), new NodeRelationVo(91L, 95L), new NodeRelationVo(92L, 96L),
            new NodeRelationVo(93L, 96L), new NodeRelationVo(94L, 96L), new NodeRelationVo(95L, 96L),
            new NodeRelationVo(96L, 97L)
        );

        return createNodeMap(nodes, relations);
    }

    private NodeMapVo createDevice10() {
        List<NodeVo> nodes = List.of(
            new NodeVo(99L, "上游装置工艺波动", null),
            new NodeVo(100L, "上游来料阀门故障", null),
            new NodeVo(101L, "人员误操作", null),
            new NodeVo(102L, "PV970801故障", null),
            new NodeVo(103L, "PV970801控制回路故障", null),
            new NodeVo(104L, "TV970602（9707020）故障", null),
            new NodeVo(105L, "TIC970614（970714）控制回路故障", null),
            new NodeVo(106L, "人员误操作", null),
            new NodeVo(107L, "TV970602（9707020）故障", null),
            new NodeVo(108L, "TIC970614（970714）控制回路故障", null),
            new NodeVo(109L, "人员误操作", null),
            new NodeVo(110L, "上游来气量压力高", null),
            new NodeVo(111L, "上游燃料气压力低", null),
            new NodeVo(112L, "PV-970801控制阀故障关小", null),
            new NodeVo(113L, "TV970602（9707020）关小", null),
            new NodeVo(114L, "TV970602(970702)全开", null),
            new NodeVo(115L, "V-9706超压", null),
            new NodeVo(116L, "回火或熄火", null),
            new NodeVo(117L, "温度升高、炉管结焦，炉管堵塞，局部干烧，压力升高", null),
            new NodeVo(118L, "泄漏", null),
            new NodeVo(119L, "燃气聚集", null),
            new NodeVo(120L, "导热油炉炉管损坏", null),
            new NodeVo(121L, "闪爆", null),
            new NodeVo(122L, "泄漏火灾", null)
        );

        List<NodeRelationVo> relations = List.of(
            new NodeRelationVo(99L, 111L), new NodeRelationVo(100L, 111L), new NodeRelationVo(101L, 112L),
            new NodeRelationVo(102L, 112L), new NodeRelationVo(103L, 112L), new NodeRelationVo(104L, 113L),
            new NodeRelationVo(105L, 113L), new NodeRelationVo(106L, 113L), new NodeRelationVo(107L, 114L),
            new NodeRelationVo(108L, 114L), new NodeRelationVo(109L, 114L), new NodeRelationVo(110L, 115L),
            new NodeRelationVo(111L, 116L), new NodeRelationVo(112L, 116L), new NodeRelationVo(113L, 116L),
            new NodeRelationVo(114L, 117L), new NodeRelationVo(115L, 117L), new NodeRelationVo(115L, 118L),
            new NodeRelationVo(116L, 119L), new NodeRelationVo(117L, 120L), new NodeRelationVo(118L, 122L),
            new NodeRelationVo(119L, 121L), new NodeRelationVo(120L, 122L)
        );

        return createNodeMap(nodes, relations);
    }

    private NodeMapVo createDevice11() {
        List<NodeVo> nodes = List.of(
            new NodeVo(123L, "人员误操作", null),
            new NodeVo(124L, "人员误操作", null),
            new NodeVo(125L, "PICA972401控制回路故障", null),
            new NodeVo(126L, "PV972401故障", null),
            new NodeVo(127L, "V-9741储罐液位高", null),
            new NodeVo(128L, "人员误操作", null),
            new NodeVo(129L, "PICA972401控制回路故障", null),
            new NodeVo(130L, "PV972401故障", null),
            new NodeVo(131L, "出口有安全阀失效", null),
            new NodeVo(132L, "PV972402B故障", null),
            new NodeVo(133L, "PICA972402控制回路故障", null),
            new NodeVo(134L, "SV-9741A/B失效", null),
            new NodeVo(135L, "PV972401故障关小", null),
            new NodeVo(136L, "二氧化碳压缩机出口阀关闭（小）", null),
            new NodeVo(137L, "二氧化碳压缩机入口进料带液", null),
            new NodeVo(138L, "PV972401故障开大", null),
            new NodeVo(139L, "二氧化碳压缩机出口压力超压", null),
            new NodeVo(140L, "下游程控阀972411故障关", null),
            new NodeVo(141L, "系统超压", null),
            new NodeVo(142L, "V-9741储罐压力高", null),
            new NodeVo(143L, "除油塔T9741压力高", null),
            new NodeVo(144L, "二氧化碳压缩机C-9741A/B泄漏", null),
            new NodeVo(145L, "V-9741储罐泄漏", null),
            new NodeVo(146L, "除油塔T9741泄漏", null),
            new NodeVo(147L, "中毒定息", null),
            new NodeVo(148L, "超压泄漏", null)
        );

        List<NodeRelationVo> relations = List.of(
            new NodeRelationVo(123L, 133L), new NodeRelationVo(124L, 135L), new NodeRelationVo(125L, 135L),
            new NodeRelationVo(126L, 135L), new NodeRelationVo(127L, 137L), new NodeRelationVo(128L, 138L),
            new NodeRelationVo(129L, 138L), new NodeRelationVo(130L, 138L), new NodeRelationVo(131L, 139L),
            new NodeRelationVo(132L, 139L), new NodeRelationVo(133L, 139L), new NodeRelationVo(134L, 139L),
            new NodeRelationVo(135L, 141L), new NodeRelationVo(136L, 141L), new NodeRelationVo(137L, 141L),
            new NodeRelationVo(138L, 142L), new NodeRelationVo(139L, 143L), new NodeRelationVo(140L, 143L),
            new NodeRelationVo(141L, 144L), new NodeRelationVo(142L, 145L), new NodeRelationVo(143L, 146L),
            new NodeRelationVo(144L, 147L), new NodeRelationVo(145L, 148L), new NodeRelationVo(146L, 148L)
        );

        return createNodeMap(nodes, relations);
    }

    private NodeMapVo createNodeMap(List<NodeVo> nodes, List<NodeRelationVo> relations) {
        NodeMapVo nodeMapVo = new NodeMapVo();
        nodeMapVo.setNodes(nodes);
        nodeMapVo.setRelations(relations);
        return nodeMapVo;
    }

    public NodeMapVo getNodeMap(Long equipmentId) {
        return NODE_MAP_CACHE.get(equipmentId);
    }

    public boolean containsEquipment(Long equipmentId) {
        return NODE_MAP_CACHE.containsKey(equipmentId);
    }
}
